package com.peak.distribution.web.agent;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.peak.distribution.entity.agent.DisAgentAccount;
import com.peak.distribution.enums.AccountType;
import com.peak.distribution.service.agent.DisAgentService;
import com.peak.distribution.vo.agent.DisAgentAccountVo;
import com.peak.spring.boot.api.web.JsonMessage;
import com.peak.spring.boot.builder.repository.FilterParamBuilder;
import com.peak.spring.boot.builder.web.JsonBuilder;
import com.peak.spring.boot.core.mapper.BeanMapper;
import com.peak.spring.boot.core.web.BaseController;

@RestController
@RequestMapping("/agent/account")
public class DisAgentAccountController extends BaseController<DisAgentAccount, DisAgentAccountVo> {
	
	@Autowired
	private DisAgentService agentService; 

	@PostMapping("/current")
	public JsonMessage getCurrentAccount(HttpServletRequest request) {
		List<DisAgentAccount> accounts = baseManager.findList(FilterParamBuilder.create().eq("agent.id", agentService.getCurrentAgentId()).build());
		Map<AccountType, DisAgentAccountVo> map = accounts.stream().collect(Collectors.toMap(DisAgentAccount::getType , p -> BeanMapper.copy(p, DisAgentAccountVo.class)));
		return JsonBuilder.message().success().content(map).build();
	}
	
	@PostMapping("/show/{id}")
	public JsonMessage getAccount(HttpServletRequest request, @PathVariable("id") Integer agentId) {
		List<DisAgentAccount> accounts = baseManager.findList(FilterParamBuilder.create().eq("agent.id", agentId).build());
		Map<AccountType, DisAgentAccountVo> map = accounts.stream().collect(Collectors.toMap(DisAgentAccount::getType , p -> BeanMapper.copy(p, DisAgentAccountVo.class)));
		return JsonBuilder.message().success().content(map).build();
	}
}
